import sseclient
import requests
import json

# SSE 服务端的 URL
SSE_SERVER_URL = "http://0.0.0.0:50000/sse"
# proxy_main 的 URL
PROXY_MAIN_URL = "http://0.0.0.0:8080/execute"

# 模拟客户端 ID
CLIENT_ID = "mcp-proxy-client-001"

# 要发送的消息
command = "tools/list"

# 构造请求数据
data = {
    "client_id": CLIENT_ID,
    "command": command
}

# 发送 POST 请求到 SSE 服务端
try:
    response = requests.post(SSE_SERVER_URL, json=data)
    if response.status_code == 200:
        print("消息已成功发送到 SSE 服务端")
        # 连接到 SSE 流
        stream_url = f"http://0.0.0.0:50000/stream?channel={CLIENT_ID}"
        response = requests.get(stream_url, stream=True)
        client = sseclient.SSEClient(response)

        for event in client.events():
            try:
                event_data = json.loads(event.data)
                if 'message' in event_data:
                    print("收到消息:")
                    print(event_data['message'])
                    break  # 收到消息后退出循环
            except json.JSONDecodeError:
                print("无法解析事件数据")
    else:
        print(f"发送消息失败，状态码: {response.status_code}, 错误信息: {response.text}")
except requests.RequestException as e:
    print(f"请求出错: {e}")